<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <el-form :inline="true" :model="dataForm">
          <el-form-item label="分类">
            <category-cascader
              :catelogPath.sync="catelogPath"
            ></category-cascader>
          </el-form-item>
          <el-form-item label="选择品牌" prop="brandId">
            <el-select
              placeholder="请选择"
              v-model="dataForm.brandId"
              filterable
              clearable
            >
              <el-option
                v-for="item in brands"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select
              style="width: 160px"
              v-model="dataForm.publishStatus"
              clearable
            >
              <el-option label="新建" :value="0"></el-option>
              <el-option label="上架" :value="1"></el-option>
              <el-option label="下架" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="检索">
            <el-input
              placeholder="名称"
              size="mini"
              style="width: 160px"
              v-model="dataForm.keyword"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="mini" @click="resetData">重置</el-button>
            <el-button type="primary" size="mini" @click="getDataList()"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      ></el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="id"
      ></el-table-column>
      <el-table-column
        prop="spuName"
        header-align="center"
        align="center"
        label="名称"
      ></el-table-column>
      <el-table-column
        prop="spuDescription"
        header-align="center"
        align="center"
        label="描述"
      ></el-table-column>
      <el-table-column
        prop="categoryId"
        header-align="center"
        align="center"
        label="分类"
      ></el-table-column>
      <el-table-column
        prop="brandId"
        header-align="center"
        align="center"
        label="品牌"
      ></el-table-column>
      <el-table-column
        prop="weight"
        header-align="center"
        align="center"
        label="重量"
      ></el-table-column>
      <el-table-column
        prop="publishStatus"
        header-align="center"
        align="center"
        label="上架状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.publishStatus == 0">新建</el-tag>
          <el-tag v-if="scope.row.publishStatus == 1">已上架</el-tag>
          <el-tag v-if="scope.row.publishStatus == 2">已下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间"
      ></el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="修改时间"
      ></el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.publishStatus == 0"
            type="text"
            size="small"
            @click="productUp(scope.row.id)"
            >上架</el-button
          >
          <el-button type="text" size="small" @click="attrUpdateShow(scope.row)"
            >规格</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[8, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next,sizes, jumper"
    ></el-pagination>
  </div>
</template>

<script>
import CategoryCascader from "../../common/category-cascader";
import SpuApi from "@/api/product/spu";

export default {
  components: { CategoryCascader },
  props: {},
  data() {
    return {
      catelogPath: [],
      brands: [],
      dataForm: {
        publishStatus: null,
        keyword: null,
        brandId: null,
        categoryId: null,
      },
      pageIndex: 1,
      pageSize: 8,
      total: 0,
      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      SpuApi.getDataList({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        ...this.dataForm,
      }).then((response) => {
        if (response && response.code === 200) {
          this.dataList = response.data.list;
          this.total = response.data.totalCount;
        }
        this.dataListLoading = false;
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 重置查询表单
    resetData() {
      this.dataForm = {};
      this.pageIndex = 1;
      this.getDataList();
    },
  },
};
</script>
<style scoped></style>
